home *** CD-ROM | disk | FTP | other *** search
/ Chip 2007 January, February, March & April / Chip-Cover-CD-2007-02.iso / Pakiet bezpieczenstwa / mini Pentoo LiveCD 2006.1 / mpentoo-2006.1.iso / livecd.squashfs / usr / lib / pygtk / 2.0 / demos / editable_cells.py < prev    next >
Text File  |  2006-01-20  |  5KB  |  167 lines

  1. #!/usr/bin/env python
  2. '''Tree View/Editable Cells
  3.  
  4. This demo demonstrates the use of editable cells in a GtkTreeView.
  5. If you're new to the GtkTreeView widgets and associates, look into the
  6. GtkListStore example first.'''
  7. # pygtk version: Maik Hertha <maik.hertha@berlin.de>
  8.  
  9. import gobject
  10. import gtk
  11.  
  12. #   columns
  13. (
  14.   COLUMN_NUMBER,
  15.   COLUMN_PRODUCT,
  16.   COLUMN_EDITABLE
  17. ) = range(3)
  18.  
  19. # data
  20. articles = [
  21.     [ 3, "bottles of coke", True ],
  22.     [ 5, "packages of noodles", True ],
  23.     [ 2, "packages of chocolate chip cookies", True ],
  24.     [ 1, "can vanilla ice cream", True ],
  25.     [ 6, "eggs", True ]
  26. ]
  27.  
  28. class EditableCellsDemo(gtk.Window):
  29.     def __init__(self, parent=None):
  30.         gtk.Window.__init__(self)
  31.         try:
  32.             self.set_screen(parent.get_screen())
  33.         except AttributeError:
  34.             self.connect('destroy', lambda *w: gtk.main_quit())
  35.         self.set_title(self.__class__.__name__)
  36.         self.set_border_width(5)
  37.         self.set_default_size(320, 200)
  38.  
  39.         vbox = gtk.VBox(False, 5)
  40.         self.add(vbox)
  41.  
  42.         label = gtk.Label("Shopping list (you can edit the cells!)")
  43.         vbox.pack_start(label, False, False)
  44.  
  45.         sw = gtk.ScrolledWindow()
  46.         sw.set_shadow_type(gtk.SHADOW_ETCHED_IN)
  47.         sw.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC)
  48.         vbox.pack_start(sw)
  49.  
  50.         # create model
  51.         model = self.__create_model()
  52.  
  53.         # create tree view
  54.         treeview = gtk.TreeView(model)
  55.         treeview.set_rules_hint(True)
  56.         treeview.get_selection().set_mode(gtk.SELECTION_SINGLE)
  57.  
  58.         self.__add_columns(treeview)
  59.  
  60.         sw.add(treeview)
  61.  
  62.         # some buttons
  63.         hbox = gtk.HBox(True, 4)
  64.         vbox.pack_start(hbox, False, False)
  65.  
  66.         button = gtk.Button(stock=gtk.STOCK_ADD)
  67.         button.connect("clicked", self.on_add_item_clicked, model)
  68.         hbox.pack_start(button)
  69.  
  70.         button = gtk.Button(stock=gtk.STOCK_REMOVE)
  71.         button.connect("clicked", self.on_remove_item_clicked, treeview)
  72.         hbox.pack_start(button)
  73.  
  74.         self.show_all()
  75.  
  76.     def __create_model(self):
  77.  
  78.         # create list store
  79.         model = gtk.ListStore(
  80.             gobject.TYPE_INT,
  81.             gobject.TYPE_STRING,
  82.             gobject.TYPE_BOOLEAN
  83.        )
  84.  
  85.         # add items
  86.         for item in articles:
  87.             iter = model.append()
  88.  
  89.             model.set (iter,
  90.                   COLUMN_NUMBER, item[COLUMN_NUMBER],
  91.                   COLUMN_PRODUCT, item[COLUMN_PRODUCT],
  92.                   COLUMN_EDITABLE, item[COLUMN_EDITABLE]
  93.            )
  94.         return model
  95.  
  96.  
  97.     def __add_columns(self, treeview):
  98.  
  99.         model = treeview.get_model()
  100.  
  101.         # number column
  102.         renderer = gtk.CellRendererText()
  103.         renderer.connect("edited", self.on_cell_edited, model)
  104.         renderer.set_data("column", COLUMN_NUMBER)
  105.  
  106.         column = gtk.TreeViewColumn("Number", renderer, text=COLUMN_NUMBER,
  107.                                editable=COLUMN_EDITABLE)
  108.         treeview.append_column(column)
  109.  
  110.         # product column
  111.         renderer = gtk.CellRendererText()
  112.         renderer.connect("edited", self.on_cell_edited, model)
  113.         renderer.set_data("column", COLUMN_PRODUCT)
  114.  
  115.         column = gtk.TreeViewColumn("Product", renderer, text=COLUMN_PRODUCT,
  116.                                editable=COLUMN_EDITABLE)
  117.         treeview.append_column(column)
  118.  
  119.  
  120.     def on_add_item_clicked(self, button, model):
  121.         new_item = [0, "Description here", True]
  122.         articles.append(new_item)
  123.  
  124.         iter = model.append()
  125.         model.set (iter,
  126.             COLUMN_NUMBER, new_item[COLUMN_NUMBER],
  127.             COLUMN_PRODUCT, new_item[COLUMN_PRODUCT],
  128.             COLUMN_EDITABLE, new_item[COLUMN_EDITABLE]
  129.        )
  130.  
  131.  
  132.     def on_remove_item_clicked(self, button, treeview):
  133.  
  134.         selection = treeview.get_selection()
  135.         model, iter = selection.get_selected()
  136.  
  137.         if iter:
  138.             path = model.get_path(iter)[0]
  139.             model.remove(iter)
  140.  
  141.             del articles[ path ]
  142.  
  143.  
  144.     def on_cell_edited(self, cell, path_string, new_text, model):
  145.  
  146.         iter = model.get_iter_from_string(path_string)
  147.         path = model.get_path(iter)[0]
  148.         column = cell.get_data("column")
  149.  
  150.         if column == COLUMN_NUMBER:
  151.             articles[path][COLUMN_NUMBER] = int(new_text)
  152.  
  153.             model.set(iter, column, articles[path][COLUMN_NUMBER])
  154.  
  155.         elif column == COLUMN_PRODUCT:
  156.             old_text = model.get_value(iter, column)
  157.             articles[path][COLUMN_PRODUCT] = new_text
  158.  
  159.             model.set(iter, column, articles[path][COLUMN_PRODUCT])
  160.  
  161. def main():
  162.     EditableCellsDemo()
  163.     gtk.main()
  164.  
  165. if __name__ == '__main__':
  166.     main()
  167.